package com.faintlight.repository;

import com.faintlight.model.KindnessChain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChainRepository extends JpaRepository<KindnessChain, Long> {
    boolean existsBySourceCapsule_Id(Long capsuleId);

    @Query("SELECT DISTINCT c FROM KindnessChain c JOIN c.nodes n WHERE n.participant.id = :userId")
    List<KindnessChain> findByParticipantId(Long userId);

    long countByNodesParticipantId(Long userId);

    @Query("SELECT DISTINCT c FROM KindnessChain c JOIN c.nodes n WHERE n.participant.id = :userId ORDER BY c.createdAt DESC")
    List<KindnessChain> findByParticipantIdOrderByCreatedAtDesc(Long userId);
}